package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yuanfeng.business.dto.ManageShopAddressAddDTO;
import com.yuanfeng.business.dto.ManageShopAddressDefaultUpdateDTO;
import com.yuanfeng.business.dto.ManageShopAddressDetailQueryDTO;
import com.yuanfeng.business.entity.ShopShippingAddressEntity;
import com.yuanfeng.business.feign.BaseServiceClient;
import com.yuanfeng.business.mapper.ShopShippingAddressMapper;
import com.yuanfeng.business.service.ShopShippingAddressService;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.dto.userms.UserTokenBeanDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.Json.JsonUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.shop.ManageShopAddressQueryVO;
import com.yuanfeng.commoms.vo.shop.ShopShippingAddressVO;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ShopShippingAddressServiceImpl
 * @Description 商家发货地址
 * @Author cf
 * @Date 2023/7/4 15:03
 * @Version 1.0
 */
@Service
public class ShopShippingAddressServiceImpl extends ServiceImpl<ShopShippingAddressMapper,ShopShippingAddressEntity> implements ShopShippingAddressService {

    @Resource
    private ShopShippingAddressMapper shopShippingAddressMapper;

    @Autowired
    private BaseServiceClient baseServiceClient;

    @Override
    public ResponseResult addShopAddress(ManageShopAddressAddDTO param) {
        // 权限判断
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByTokenNoIsExist(param.getToken());
        if (userInfoVO == null){
            userInfoVO = ParseTokenUtils.getPlatformUser(param.getToken());
            if (userInfoVO != null){
                userInfoVO.setShopId("0");
            }
        }
        if (userInfoVO != null) {
            Integer shopId = Integer.valueOf(userInfoVO.getShopId());
            param.setShopId(shopId);
            param.setShippingAddressTime(new Date());
            if (param.getShippingAddressDefault() != null && param.getShippingAddressDefault().equals(1)) {
                shopShippingAddressMapper.updateAddressDefault2(shopId);
            }
            if (null != param.getShippingAddressId()) {
                shopShippingAddressMapper.updateAddress(param);
            } else {
                shopShippingAddressMapper.insertAddress(param);
            }
        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success();
    }


    @Override
    public ResponseResult deleteShopAddress(ManageShopAddressDetailQueryDTO param) {
        // 权限判断
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (userInfoVO != null) {
            shopShippingAddressMapper.deleteByPrimaryKey(param.getShippingAddressId());

        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success();
    }


    @Override
    public ResponseResult queryShopAddressDetail(ManageShopAddressDetailQueryDTO param) {
        ManageShopAddressQueryVO shopAddress = new ManageShopAddressQueryVO();
        // 权限判断
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (userInfoVO != null) {
            shopAddress = shopShippingAddressMapper.selectShopAddressDetail(param.getShippingAddressId());
            // 设置店铺地址名称
            setShopAddressName(shopAddress);

        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success(shopAddress);
    }

    /**
     * 设置店铺发货地址的名称
     *
     * @param shopAddress
     */
    private void setShopAddressName(ManageShopAddressQueryVO shopAddress) {
        List<Integer> districtIds = Lists.newArrayList();
        districtIds.add(shopAddress.getShippingAddressProvinceId());
        districtIds.add(shopAddress.getShippingAddressCityId());
        districtIds.add(shopAddress.getShippingAddressAreaId());
        List<String> districtNames = baseServiceClient.queryDistrictName(districtIds);
        String shippingAddressArea = "";
        for (String districtName : districtNames) {
            shippingAddressArea = shippingAddressArea + districtName;
        }
        shopAddress.setShippingAddressArea(shippingAddressArea);
    }

    @Override
    public ResponseResult updateShopAddressDefault(ManageShopAddressDefaultUpdateDTO param) {
        // 权限判断
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (userInfoVO != null) {
            shopShippingAddressMapper.updateAddressDefault(param.getShippingAddressId(),
                    Integer.valueOf(userInfoVO.getShopId()));
            shopShippingAddressMapper.updateAddressNoDefault(param.getShippingAddressId(),
                    Integer.valueOf(userInfoVO.getShopId()));

        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryShippingAddress(Integer shippingAddressId) {
        ShopShippingAddressEntity shopAddress = this.baseMapper.selectOne(
                new LambdaUpdateWrapper<ShopShippingAddressEntity>()
                        .eq(ShopShippingAddressEntity::getShippingAddressId, shippingAddressId));
        if (null == shopAddress) {
            return ResponseResult.fail(0, "shippingAddressId = " + shippingAddressId + "不存在");
        }
        List<Integer> districtIdList = Lists.newArrayList();
        districtIdList.add(shopAddress.getShippingAddressProvinceId());
        districtIdList.add(shopAddress.getShippingAddressCityId());
        districtIdList.add(shopAddress.getShippingAddressAreaId());
        //根据区域id集合查找区域名字集合
        List<String> shopShippingAddressList = baseServiceClient.selectDistrictNameListByIds(districtIdList);
        ShopShippingAddressVO result = new ShopShippingAddressVO();
        for (int i = 0; i < shopShippingAddressList.size(); i++) {
            if (i == 0) {
                result.setShippingAddressProvince(shopShippingAddressList.get(i));
            } else if (i == 1) {
                result.setShippingAddressCity(shopShippingAddressList.get(i));
            } else {
                result.setShippingAddressArea(shopShippingAddressList.get(i));
            }
        }
        return ResponseResult.success(JsonUtils.toString(result));

    }

    @Override
    public ResponseResult selectAddress(UserTokenBeanDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            ShopShippingAddressEntity manageShopAddressAddParam = this.baseMapper.selectOne(new LambdaQueryWrapper<ShopShippingAddressEntity>()
                                                                 .eq(ShopShippingAddressEntity::getShopId,0));
            if(null != manageShopAddressAddParam){
                // 设置店铺地址名称
                String completeAddress = baseServiceClient.getAddressString(manageShopAddressAddParam.getShippingAddressProvinceId(),
                        manageShopAddressAddParam.getShippingAddressCityId(), manageShopAddressAddParam.getShippingAddressAreaId()).getData().toString();
                manageShopAddressAddParam.setShippingAddressArea(completeAddress);
            }

            return ResponseResult.success(manageShopAddressAddParam);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult queryShopAddressList(UserTokenBeanDTO param) {
        // 权限判断
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (userInfoVO == null) {
            return ResponseResult.fail(10086, "没有权限");
        }
        List<ManageShopAddressQueryVO> shopAddresss = shopShippingAddressMapper.selectShopAddressList(Integer.valueOf(userInfoVO.getShopId()));
        for (ManageShopAddressQueryVO shopAddress : shopAddresss) {
            // 设置店铺地址名称
            setShopAddressName(shopAddress);
        }
        return ResponseResult.success(shopAddresss);
    }


}
